Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@endo/stream
Advanced tools
Endo models streams as hardened async iterators. Async iterators are sufficient to model back-pressure or pacing since they are channel messages both from producer to consumer and consumer to producer. Streams are therefore symmetric. The same stream type serves for both a reader and a writer.
These streams depend on full Endo environment initialization, as with @endo/init
to ensure that they are run in Hardened JavaScript with remote promise support
(eventual send).
To write to a stream, give a value to the next method.
// ...
await writer.next(value);
Awaiting the returned promise slows the writer to match the pace of the reader.
To read from a stream, await the value returned by the next method.
for await (const value of reader) {
// ...
}
To map a reader to a reader through a synchronous value transform, use mapReader
.
const doubleReader = mapReader(singleReader, n => n * 2);
In this example, any value read from doubleReader will be double what was read from singleReader.
To map a writer to a writer through a synchronous value transform, use
mapWriter
.
const singleWriter = mapWriter(doubleWriter, n => n * 2);
In this example, any value written to singleWriter will be writ double to doubleWriter.
The makePipe
function returns an entangled pair of streams.
Use one as a reader and the other as a writer.
Pipes are useful for mocking streams in tests.
const [writer, reader] = makePipe();
Pipes use makeStream
and makeQueue
.
makeQueue
creates an async promise queue: a collection type like a queue
except that get
returns a promise and put
accepts a promise, so get
can
be called before put
.
An async queue ensures that the promises returned by get
and accepted by
put
are matched respectively, but provides no guarantee about the order in
which promises settle.
A stream is consequently a pair of queues that transport iteration results,
one to send messages forward and another to receive acknowledgements.
The pump
function pumps iterations from a reader to a writer.
The pump must be primed with the first acknowledgement to send to the reader,
typically undefined
, as in reader.next(undefined)
.
This makes the parity of a pump "odd", because the reader needs a free
acknowledgement to start.
This is in contrast to a pipe, which has "even" parity, because the reader and
writer can both proceed initially.
So, for example, we can implement cat
in Node.js by pumping stdin to stdout.
import { makeNodeWriter, makeNodeReader } from '@endo/stream-node';
const writer = makeNodeWriter(process.stdout);
const reader = makeNodeReader(process.stdin);
await pump(writer, reader);
Async generator functions are very useful for making reader adapters.
async function *double(reader) {
for await (const value of reader) {
yield value * 2;
}
return undefined;
}
However, async generator functions can also serve as writers, because yield
evaluates to the argument passed to next
.
However, generator writers have odd parity, meaning the first value sent to a
generator function has nowhere to go and gets discarded as the program counter
proceeds from the beginning of the function to the first yield
, return
, or
throw
.
The prime
function compensates for this by sending a primer to the generator
once.
async function *logGenerator() {
for (;;) {
console.log(yield);
}
}
const writer = prime(logGenerator());
await writer.next('First message is not discarded');
This library depends on Hardened JavaScript. The environment must be locked down before initializing this library. All of the exported functions and the streams they produce are hardened.
This implementation of streams ensures that iteration results are shallowly frozen. The user is responsible for hardening the transported values if that is their intent. Some values like array buffers cannot be frozen.
FAQs
Foundation for async iterators as streams
We found that @endo/stream demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.